/**
 * Created by v on 1/1/14.
 */


$(function() {

    // start datetimepicker use
    $(".js-datetimepicker").datetimepicker({
        autoclose: 1,
        //        pickDate: false
        format: "yyyy-mm-dd",
        //        pickDate: true,
        pickTime: false,
        todayBtn: "linked"
        // pick12HourFormat: true
        // language: "en"
    });

    var $addimages = $("#jkaddimages");
    $addimages.children("button").click(function() {
        // send message when enter click
        var string = generateStringOfAdd($addimages);
        if (string.length > 0) {
            jk_send_request_post("/jmdkina/AddItem.html", string, resultAddImages);
        }
    });

    //list.html del item
    var $delimage = $(".js-delitem");
    $delimage.click(function() {
        $(this).after("<div id='jkconfirm' >密码<input class='form-control' name='code' type='password'><button class='btn btn-danger'>确定</button></div>");
        $(this).siblings("#jkconfirm").find("button").click(function() {
            var code = $(this).siblings("input[name=code]").val();
            var title = $(this).parent().siblings("div[name=title]").html();
            var string = "jk=" + "&code=" + code + "&title=" + title;
            //alert(string);
            jk_send_request_post("/jmdkina/DelItem.html", string, resultDelImage);
            $("#jkconfirm").remove();
        });
    });
});

function resultAddImages(data, textStatus) {
    if (textStatus != "success") {
        alert("添加条目失败");
        return false;
    }
    var result = $.parseJSON(data.responseText);
    alert(result.status);
}

function resultDelImage(data, textStatus) {
    if (textStatus != "success") {
        alert("删除条目失败");
        return false;
    }
    var result = $.parseJSON(data.responseText);
    alert(result.status);
}

function generateStringOfAdd(id) {
    // get title of time
    var time = id.find("input[name=title]").val();
    var author = id.find("input[name=author]").val().trim();
    var content = id.find("textarea[name=content]").val().trim();
    var code = id.find("input[name=code]").val().trim();
    if (time.length == 0 || author.length == 0 || content.length == 0 || code.length == 0) {
        alert("都是要填写的^.^");
        return "";
    }
    var string = "jk=&" + "title=" + time + "&author=" + author + "&content=" + content + "&code=" + code;
    //    alert(string);
    return string;
}